import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

class FinanceVisualizer:
    @staticmethod
    def plot_spending_trend(data: dict):
        """生成消费趋势图"""
        sns.set_style("whitegrid")
        fig, ax = plt.subplots(figsize=(10, 6))
        
        categories = [item['category'] for item in data]
        amounts = [item['SUM(amount)'] for item in data]
        
        sns.barplot(x=categories, y=amounts, palette="viridis")
        ax.set_title("月度消费分类汇总")
        ax.set_ylabel("金额（元）")
        
        plt.xticks(rotation=45)
        plt.tight_layout()
        plt.savefig("monthly_report.png")
        plt.close()

    @staticmethod
    def plot_annual_comparison(data: dict):
        """生成年度消费对比图"""
        sns.set_style("whitegrid")
        fig, ax = plt.subplots(figsize=(12, 7))
        
        years = list(data.keys())
        categories = list(data[years[0]].keys())
        values = [[data[year][category] for year in years] for category in categories]
        
        x = np.arange(len(years))
        width = 0.15
        
        for i, category in enumerate(categories):
            ax.bar(x + i*width, values[i], width, label=category)
        
        ax.set_xlabel('年份')
        ax.set_ylabel('消费金额（元）')
        ax.set_title('年度消费对比分析')
        ax.set_xticks(x + width*(len(categories)-1)/2)
        ax.set_xticklabels(years)
        ax.legend()
        
        plt.tight_layout()
        plt.savefig('annual_comparison.png')
        plt.close()

    @staticmethod
    def plot_spending_pie(data: dict):
        """生成消费占比饼图"""
        sns.set_style("whitegrid")
        fig, ax = plt.subplots(figsize=(10, 10))
        
        categories = [item['category'] for item in data]
        amounts = [item['SUM(amount)'] for item in data]
        
        ax.pie(amounts, labels=categories, autopct='%1.1f%%', startangle=90)
        ax.axis('equal')
        ax.set_title('消费类别占比分析')
        
        plt.tight_layout()
        plt.savefig('spending_pie.png')
        plt.close()